﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Globalization
{
    public partial class ucMain : UserControl
    {
        public ucMain()
        {
            Debug.WriteLine(DateTime.Now + " DEBUG: Plugin Globalization.dll běží v {0} s  AppDomainID = {1}.", Thread.GetDomain().FriendlyName, Thread.GetDomainID());

            InitializeComponent();

            // find all cultures
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

            // fill dropdownlist and remember actual culture's index
            int selIdnex = 0;
            foreach (CultureInfo culture in cultures)
            {
                comboCultures.Items.Add(new MyClutureInfo(culture));
                if (culture.Name == Thread.CurrentThread.CurrentCulture.Name)
                    selIdnex = comboCultures.Items.Count - 1;
            }
            comboCultures.SelectedIndex = selIdnex;
        }

        class MyClutureInfo
        {
            public CultureInfo Culture;
            public MyClutureInfo(CultureInfo cult)
            {
                Culture = cult;
            }
            public override string ToString()
            {
                return Culture.DisplayName;
            }
        }

        private void comboCultures_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // Zjisti vybranou culture
            CultureInfo selected = ((MyClutureInfo)comboCultures.SelectedItem).Culture;

            try
            {
                // nastavi hlavnimu threadu danou culture
                Thread.CurrentThread.CurrentCulture =
                    CultureInfo.CreateSpecificCulture(selected.Name);
            }
            catch
            {
                // doslo k chybe - nektere cultures nelze pouzit,
                // protoze .NET ma vetsi seznam nez WinAPI, ktere se zde
                // interne pouzivaji
                // 
                // ..napriklad zh-CHT (Chinese Traditional)
                MessageBox.Show(string.Format(
                    "Culture {0} isn't supported!\nPlease select another.", selected.Name),
                    selected.DisplayName);
            }
            // Zobrazit znovu data
            DisplayGlobalizedData();
        }

        private void DisplayGlobalizedData()
        {
            lblFloatNumber.Text = Math.PI.ToString();

            // days
            string tmp = "";
            foreach (string dayName in Thread.CurrentThread.
                CurrentCulture.DateTimeFormat.DayNames)
            {
                tmp += ", " + dayName;
            }
            lblDayNames.Text = tmp.Substring(2);

            // months
            tmp = "";
            for (int i = 0; i < 12; i++)
            {
                string monthName = Thread.CurrentThread.
                    CurrentCulture.DateTimeFormat.MonthNames[i];
                tmp += ", " + monthName;
            }
            lblMonthNames.Text = tmp.Substring(2);

            // refresh time
            tmrRefresh_Tick(null, EventArgs.Empty);
        }

        private void tmrRefresh_Tick(object sender, System.EventArgs e)
        {
            lblDateTime.Text = DateTime.Now.ToString();
        }


    }
}
